using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms;
using System.Collections;

namespace Jonathan.AlphaGradientPanel
{
    [TypeConverter(typeof(ColorWithAlphaCollectionConverter)), Editor(typeof(ColorWithAlphaCollectionEditor), typeof(UITypeEditor)), DesignTimeVisible(false), ToolboxItem(false), System.Serializable()]
    public class ColorWithAlphaCollection : CollectionBase, ICustomTypeDescriptor
    {
        private Control ctlParent;

        public ColorWithAlphaCollection ()
        {

        }

        public ColorWithAlphaCollection (Control ParentControl)
        {
            Parent = ParentControl;
        }

        [Browsable(false)]
        public Control Parent
        {
            get { return ctlParent; }
            set { ctlParent = value; }
        }

        protected override void OnSet (int index, object oldValue, object newValue)
        {
            ((ColorWithAlpha)newValue).Parent = Parent;
            base.OnSet(index, oldValue, newValue);
        }

        protected override void OnInsert (int index, object value)
        {
            ((ColorWithAlpha)value).Parent = Parent;
            base.OnInsert(index, value);
        }

        public ColorWithAlpha this[int index]
        {
            get { return (ColorWithAlpha)List[index];}
        }

        /*public ColorWithAlpha Item
        {
            get { return (ColorWithAlpha)List[index]; }
        }*/

        public ColorWithAlpha Add (ColorWithAlpha value)
        {
            List.Add(value);
            return value;
        }

        public bool Contains (ColorWithAlpha value)
        {
            return List.Contains(value);
        }

        public void Remove (ColorWithAlpha value)
        {
            List.Remove(value);
        }

        public int IndexOf (ColorWithAlpha value)
        {
            return List.IndexOf(value);
        }

        public void Insert (int index, ColorWithAlpha value)
        {
            List.Insert(index, value);
        }

        public new void Clear ()
        {
            base.Clear();
        }

        public System.ComponentModel.AttributeCollection GetAttributes ()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        public string GetClassName ()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        public string GetComponentName ()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        public System.ComponentModel.TypeConverter GetConverter ()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        public System.ComponentModel.EventDescriptor GetDefaultEvent ()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        public System.ComponentModel.PropertyDescriptor GetDefaultProperty ()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        public object GetEditor (System.Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        public System.ComponentModel.EventDescriptorCollection GetEvents ()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        public System.ComponentModel.EventDescriptorCollection GetEvents (System.Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        public System.ComponentModel.PropertyDescriptorCollection GetProperties ()
        {
            return null;
        }

        public System.ComponentModel.PropertyDescriptorCollection GetProperties (System.Attribute[] attributes)
        {
            PropertyDescriptorCollection pdcProperties = new PropertyDescriptorCollection(null);
            for (int i = 0; i <= List.Count - 1; i++)
            {
                ColorWithAlphaCollectionPropertyDescriptor pdProperty = new ColorWithAlphaCollectionPropertyDescriptor(this, i);
                pdcProperties.Add(pdProperty);
            }
            return pdcProperties;
        }

        public object GetPropertyOwner (System.ComponentModel.PropertyDescriptor pd)
        {
            return this;
        }

    } 
}
